package com.gomore.opple.module.searchgoods;

import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.goods.DataDict;
import com.gomore.opple.rest.hottag.HotFilter;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class SearchGoodsPresenter implements SearchGoodsContract.Presenter {

    private DataRepository mDataRepositroy;
    private final SearchGoodsContract.View mView;
    private CompositeSubscription mSubscriptions;
    private List<DataDict> hotFilterList = new ArrayList<>();
    private List<String> hostoryList = new ArrayList<>();

    @Inject
    SearchGoodsPresenter(DataRepository dataRepository, SearchGoodsContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {
        if (mDataRepositroy.getSearchKey() != null) {
            hostoryList.addAll(mDataRepositroy.getSearchKey());
            mView.showHistoryFilter();
        }
    }

    @Override
    public void saveSearchKey(String key) {
        List<String> keyList = new ArrayList<>();
        if (mDataRepositroy.getSearchKey() != null) {
            keyList.addAll(mDataRepositroy.getSearchKey());
        }
        keyList.add(key);
        //除去重复的值
        Set<String> setString = new HashSet<>();
        setString.addAll(keyList);
        keyList.clear();
        keyList.addAll(setString);
        //保存
        mDataRepositroy.saveSearchKey(keyList);
    }

    @Override
    public void getHotTag() {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.getHotTag()
                .map(new ServerResponseFunc<HotFilter>())
                .onErrorResumeNext(new HttpResponseFunc<HotFilter>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<HotFilter>() {
                    @Override
                    public void onNext(HotFilter hotFilter) {
                        super.onNext(hotFilter);
                        mView.hideProgressDialog();
                        hotFilterList.addAll(hotFilter.getList());
                        mView.showHotTag();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public List<DataDict> getHotTagData() {
        return hotFilterList;
    }

    @Override
    public List<String> getHistoryData() {
        return hostoryList;
    }

    @Override
    public void clearHistory() {
        mDataRepositroy.clearHistory();
        hostoryList.clear();
        mView.showHistoryFilter();
    }
}
